/**
 * RickPan Electron类型定义
 */

// 文件选择选项
export interface FileSelectOptions {
  multiple?: boolean
  filters?: Array<{
    name: string
    extensions: string[]
  }>
}

// 通知选项
export interface NotificationOptions {
  title: string
  body: string
  icon?: string
  silent?: boolean
  urgency?: 'normal' | 'critical' | 'low'
}

// 更新信息
export interface UpdateInfo {
  version: string
  releaseDate: string
  releaseNotes?: string
  downloadUrl?: string
  fileSize?: number
}

// 窗口状态
export interface WindowState {
  x?: number
  y?: number
  width: number
  height: number
  isMaximized: boolean
  isFullScreen: boolean
}

// 应用配置
export interface AppConfig {
  autoLaunch: boolean
  minimizeToTray: boolean
  closeToTray: boolean
  autoUpdate: boolean
  theme: 'light' | 'dark' | 'system'
  language: string
}

// 托盘菜单项
export interface TrayMenuItem {
  label: string
  type?: 'normal' | 'separator' | 'submenu' | 'checkbox' | 'radio'
  click?: () => void
  enabled?: boolean
  visible?: boolean
  checked?: boolean
  submenu?: TrayMenuItem[]
}

// 全局快捷键配置
export interface GlobalShortcut {
  key: string
  action: string
  description: string
  enabled: boolean
}

// 文件拖拽事件
export interface FileDragEvent {
  files: string[]
  position: {
    x: number
    y: number
  }
}

// IPC消息类型
export type IPCMessage = 
  | { type: 'file:select'; payload: FileSelectOptions }
  | { type: 'file:selectFolder' }
  | { type: 'file:save'; payload: { data: any; defaultPath?: string } }
  | { type: 'app:minimize' }
  | { type: 'app:maximize' }
  | { type: 'app:close' }
  | { type: 'app:hide' }
  | { type: 'app:show' }
  | { type: 'system:openExternal'; payload: string }
  | { type: 'system:showItemInFolder'; payload: string }
  | { type: 'notification:show'; payload: NotificationOptions }
  | { type: 'updater:check' }
  | { type: 'updater:download' }
  | { type: 'updater:install' }

// Electron API接口
export interface ElectronAPI {
  file: {
    select: (options?: FileSelectOptions) => Promise<string[]>
    selectFolder: () => Promise<string | null>
    save: (data: any, defaultPath?: string) => Promise<string | null>
  }
  
  app: {
    minimize: () => Promise<void>
    maximize: () => Promise<void>
    close: () => Promise<void>
    hide: () => Promise<void>
    show: () => Promise<void>
    getVersion: () => string
  }
  
  system: {
    openExternal: (url: string) => Promise<void>
    showItemInFolder: (path: string) => Promise<void>
    getPlatform: () => string
    isDev: () => boolean
  }
  
  notification: {
    show: (options: NotificationOptions) => Promise<void>
    checkPermission: () => string
  }
  
  updater: {
    checkForUpdates: () => Promise<void>
    downloadUpdate: () => Promise<void>
    installUpdate: () => Promise<void>
    onUpdateAvailable: (callback: (info: UpdateInfo) => void) => void
    onUpdateDownloaded: (callback: (info: UpdateInfo) => void) => void
    removeUpdateListeners: () => void
  }
  
  drag: {
    enableFileDrop: (callback: (files: string[]) => void) => void
  }
  
  clipboard: {
    writeText: (text: string) => void
    readText: () => Promise<string>
  }
  
  devTools: {
    open: () => void
    close: () => void
  }
  
  env: {
    isElectron: boolean
    isDev: boolean
    platform: string
    arch: string
    nodeVersion: string
    electronVersion: string
    chromeVersion: string
  }
}

// 扩展Window接口
declare global {
  interface Window {
    electronAPI: ElectronAPI
    isElectron: boolean
    versions: {
      node: string
      chrome: string
      electron: string
    }
  }
}

// 主进程事件类型
export interface MainProcessEvents {
  'window-created': (window: Electron.BrowserWindow) => void
  'window-closed': (window: Electron.BrowserWindow) => void
  'app-ready': () => void
  'app-quit': () => void
  'update-available': (info: UpdateInfo) => void
  'update-downloaded': (info: UpdateInfo) => void
  'file-dropped': (files: string[]) => void
}

// 渲染进程事件类型
export interface RendererProcessEvents {
  'file-selected': (files: string[]) => void
  'folder-selected': (folder: string) => void
  'notification-clicked': () => void
  'update-progress': (progress: number) => void
  'app-focus': () => void
  'app-blur': () => void
}

// 错误类型
export interface ElectronError {
  code: string
  message: string
  stack?: string
  details?: any
}

// 应用状态
export interface AppState {
  isReady: boolean
  windows: Electron.BrowserWindow[]
  tray: Electron.Tray | null
  menu: Electron.Menu | null
  config: AppConfig
  shortcuts: GlobalShortcut[]
}

// 构建配置
export interface BuildConfig {
  appId: string
  productName: string
  directories: {
    output: string
    buildResources?: string
  }
  files: string[]
  extraFiles?: string[]
  mac?: {
    category: string
    target: string
    icon?: string
  }
  win?: {
    target: string
    icon?: string
  }
  linux?: {
    target: string
    category: string
    icon?: string
  }
  publish?: {
    provider: string
    owner?: string
    repo?: string
  }
}

export default ElectronAPI
